<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
    
      <el-form-item label="状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="状态"
          clearable
          size="small"
          style="width: 240px"
        >
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
        <el-form-item label="股票分类" prop="stockType">
        <el-select v-model="queryParams.stockType" placeholder="请选择股票分类" clearable size="small">
          <el-option
            v-for="dict in stockTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['yf:my:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['yf:my:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['yf:my:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['yf:my:export']"
        >导出</el-button>
      </el-col>
    </el-row>

 <el-tag
            type="danger"
            disable-transitions
          >{{showSzzs}}</el-tag>
<br>
    <el-table v-loading="loading" :data="myList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="名称" align="center" prop="name" width="150" />
      <el-table-column
        label="当前价格"
        align="center"
        prop="curPrice"
        :formatter="curInfoFormat"
        width="100"
      >
        <template slot-scope="scope">
          <el-tag
            effect="dark"
            :type="scope.row.percent>0 ? 'danger' : 'success'"
            disable-transitions
          >{{scope.row.curPrice}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="当前%" align="center" prop="percent" width="100">
        <template slot-scope="scope">
          <el-tag
            effect="dark"
            :type="scope.row.percent>0 ? 'danger' : 'success'"
            disable-transitions
          >{{scope.row.percent}}</el-tag>
        </template>
      </el-table-column>
       <el-table-column label="盈亏%" align="center" prop="earnPercent" width="100">
        <template slot-scope="scope">
          <el-tag
            effect="dark"
            :type="scope.row.earnPercent>0 ? 'danger' : 'success'"
            disable-transitions
          >{{scope.row.earnPercent}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="技术分析" align="left" prop="remark" >
        <template slot-scope="scope">
          <el-tag
            :type="scope.row.percent>0 ? 'danger' : 'success'"
            disable-transitions
          >{{scope.row.remark}}</el-tag>
        </template>
      </el-table-column>
      <!-- <el-table-column label="均线信息" align="left" prop="avgLineInfo" >
          <template slot-scope="scope">
          <el-tag
            :type="scope.row.percent>0 ? 'danger' : 'success'"
            disable-transitions
          >{{"红:" +
        scope.row.red +
        "    绿:" +
        scope.row.green +
        " 生命:" +
        scope.row.life +
        " 上轨:" +
        scope.row.upper +
        " 下轨:" +
        scope.row.lower}}</el-tag>
        </template>
      </el-table-column> -->
      <!-- <el-table-column label="绿线提醒" align="center" prop="remindGreen" :formatter="remindGreenFormat" />
      <el-table-column label="生命线提醒" align="center" prop="remindLife" :formatter="remindLifeFormat" />
      <el-table-column label="上轨提醒" align="center" prop="remindUpper" :formatter="remindUpperFormat" />
      <el-table-column label="下轨提醒" align="center" prop="remindLower" :formatter="remindLowerFormat" />
      <el-table-column label="状态" align="center" prop="status" :formatter="statusFormat" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['yf:my:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['yf:my:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改我的股票对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称" :disabled="this.form.id>0" />
        </el-form-item>
          <el-form-item label="成本" prop="cost">
        <el-input
          maxlength="5"
          oninput="value=value.replace(/[^\d.]/g,'')"
          v-model="form.cost"
          placeholder="请输入成本"
          style="width:100%"
          clearable
        ></el-input>
      </el-form-item>
        <el-form-item label="股票分类">
          <el-select v-model="form.stockType" placeholder="请选择股票分类">
            <el-option
              v-for="dict in stockTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-divider content-position="left">提醒</el-divider>
        <el-form-item label="红线">
          <el-radio-group v-model="form.remindRed">
            <el-radio
              v-for="dict in remindRedOptions"
              :key="dict.dictValue"
              :label="parseInt(dict.dictValue)"
            >{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="绿线">
          <el-radio-group v-model="form.remindGreen">
            <el-radio
              v-for="dict in remindGreenOptions"
              :key="dict.dictValue"
              :label="parseInt(dict.dictValue)"
            >{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="生命线">
          <el-radio-group v-model="form.remindLife">
            <el-radio
              v-for="dict in remindLifeOptions"
              :key="dict.dictValue"
              :label="parseInt(dict.dictValue)"
            >{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="上轨">
          <el-radio-group v-model="form.remindUpper">
            <el-radio
              v-for="dict in remindUpperOptions"
              :key="dict.dictValue"
              :label="parseInt(dict.dictValue)"
            >{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="下轨">
          <el-radio-group v-model="form.remindLower">
            <el-radio
              v-for="dict in remindLowerOptions"
              :key="dict.dictValue"
              :label="parseInt(dict.dictValue)"
            >{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in statusOptions"
              :key="dict.dictValue"
              :label="dict.dictValue"
            >{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listMy, getMy, delMy, addMy, updateMy, exportMy,szzsMy } from "@/api/yf/my";

export default {
  name: "My",
  data() {
    return {
      showSzzs:"",
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 我的股票表格数据
      myList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 红线提醒字典
      remindRedOptions: [],
      // 绿线提醒字典
      remindGreenOptions: [],
      // 生命线提醒字典
      remindLifeOptions: [],
      // 上轨提醒字典
      remindUpperOptions: [],
      // 下轨提醒字典
      remindLowerOptions: [],
      // 状态字典
      statusOptions: [],
       // 股票分类字典
      stockTypeOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined,
        status: "0",
        stockType:"1"
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  created() {
     this.getDicts("yf_stock_type").then(response => {
      this.stockTypeOptions = response.data;
    });

    this.getList();
    szzsMy().then(res=>{
      this.showSzzs=res;
    });
    this.getDicts("yf_stock_notice").then(response => {
      this.remindRedOptions = response.data;
    });
    this.getDicts("yf_stock_notice").then(response => {
      this.remindGreenOptions = response.data;
    });
    this.getDicts("yf_stock_notice").then(response => {
      this.remindLifeOptions = response.data;
    });
    this.getDicts("yf_stock_notice").then(response => {
      this.remindUpperOptions = response.data;
    });
    this.getDicts("yf_stock_notice").then(response => {
      this.remindLowerOptions = response.data;
    });
    this.getDicts("sys_normal_disable").then(response => {
      this.statusOptions = response.data;
    });
  },
  methods: {
    /** 查询我的股票列表 */
    getList() {
      this.loading = true;
      listMy(this.queryParams).then(response => {
        this.myList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    curInfoFormat(row, column) {
      return row.curPrice;
    },

    // 红线提醒字典翻译
    avgLineInfoFormat(row, column) {
      return (
        "红:" +
        row.red +
        "    绿:" +
        row.green +
        " 生命:" +
        row.life +
        " 上轨:" +
        row.upper +
        " 下轨:" +
        row.lower
      );
    },
    // 绿线提醒字典翻译
    remindGreenFormat(row, column) {
      return this.selectDictLabel(this.remindGreenOptions, row.remindGreen);
    },
    // 生命线提醒字典翻译
    remindLifeFormat(row, column) {
      return this.selectDictLabel(this.remindLifeOptions, row.remindLife);
    },
    // 上轨提醒字典翻译
    remindUpperFormat(row, column) {
      return this.selectDictLabel(this.remindUpperOptions, row.remindUpper);
    },
    // 下轨提醒字典翻译
    remindLowerFormat(row, column) {
      return this.selectDictLabel(this.remindLowerOptions, row.remindLower);
    },
    // 状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        name: undefined,
        stockId: undefined,
        remindRed: 0,
        remindGreen: 0,
        remindLife: 0,
        remindUpper: 0,
        remindLower: 0,
        status: "0",
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        createUid: undefined,
        updateUid: undefined,
        stockType: "1"

      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加我的股票";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getMy(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改我的股票";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateMy(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addMy(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm(
        '是否确认删除我的股票编号为"' + ids + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      )
        .then(function() {
          return delMy(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有我的股票数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(function() {
          return exportMy(queryParams);
        })
        .then(response => {
          this.download(response.msg);
        })
        .catch(function() {});
    }
  }
};
</script>